package com.tianyang.yukuaifu.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tianyang.yukuaifu.R;

/**
 * Description：最大高度
 * Created by 薛勇军 on 2023/7/11.
 */
public class MaxHeightLinearLayout extends LinearLayout {

    private int mHeight;

    public MaxHeightLinearLayout(@NonNull Context context) {
        super(context);
        initAttr(context, null, 0);
    }

    public MaxHeightLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttr(context, attrs, 0);
    }

    public MaxHeightLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initAttr(context, attrs, defStyle);
    }

    public void setMaxHeight(int height) {
        mHeight = height;
        invalidate();
    }

    private void initAttr(Context context, AttributeSet attrs, int defStyle) {
        final TypedArray appearance = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightLinearLayout, defStyle, 0);
        mHeight = appearance.getDimensionPixelSize(R.styleable.MaxHeightLinearLayout_rv_max_height, 0);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        if(mHeight > 0) {
            heightSpec = MeasureSpec.makeMeasureSpec(mHeight, MeasureSpec.AT_MOST);
        }
        super.onMeasure(widthSpec, heightSpec);
    }
}
